import c_screener_mode
import Room
import pygame
import SimpleCamera
import Character
import DECISIONS

aim=pygame.image.load("aim.bmp")
aim.set_colorkey((255,0,255))
healthbar_image=pygame.image.load("healthbar_1.jpg")

class cDungeonPlay(c_screener_mode.c_screener_mode):
	def activate(self):
		import Crossbow
		import BasicBomb
		import Stone
		import ThrowingAxe
		import Alteration
		import GlassClone
		import GraniteWall
		import Blowgun
		import Javelin
		self.testRoom=Room.getTestRoom()
		self.hero1=Character.Character()
		#self.hero1.weaponEquipped=Crossbow.Crossbow()
		#self.hero1.weaponEquipped=BasicBomb.BasicBomb()
		#self.hero1.weaponEquipped=Stone.Stone()
		#self.hero1.weaponEquipped=ThrowingAxe.ThrowingAxe()
		#self.hero1.weaponEquipped=Blowgun.Blowgun()
		self.hero1.weaponEquipped=Javelin.Javelin()
		self.hero1.magickEquipped=Alteration.Alteration()
		#self.hero1.magickEquipped=GlassClone.GlassClone()
		#self.hero1.magickEquipped=GraniteWall.GraniteWall()
		self.testRoom.addAnotherCharacter(self.hero1)
		self.hero1.fraction=1
		self.hero1.canCollect=True
		import VI
		charr=Character.Character(x=350)
		charr.fraction=4
		#charr.weaponEquipped=Crossbow.Crossbow()
		self.testRoom.addAnotherCharacter(charr)
		self.vi=VI.VI(fosterling=charr)
	def __init__(self):
		c_screener_mode.c_screener_mode.__init__(self)
		self.camera1=SimpleCamera.SimpleCamera()
		self.pause_submenu=None
	def do(self):
		if not self.hero1.dead:
			self.camera1.recenterCamera([self.hero1.getActingSelf()])
		events=pygame.event.get()
		print pygame.display.get_active()
		mouse_pos=pygame.mouse.get_pos()
		self.vi.decide()
		if not self.hero1.dead and pygame.mouse.get_pressed()[2]:
			self.hero1.acceptDecision(DECISIONS.MAGICK_ATTACK_SUSTAIN)
		if not self.hero1.dead:
			look=(mouse_pos[0]-0.5*self.window_size[0],-mouse_pos[1]+0.5*self.window_size[1])
			if look==(0.0,0.0):
				look=(0.0,1.0)
			llook=look[0]**2+look[1]**2
			if llook!=1:
				llook=1/(llook*0.5)
				look=(look[0]*llook,look[1]*llook)
				self.hero1.setLook(look)
		kp_events=pygame.key.get_pressed()
		#print pygame.mouse.get_pressed()
		if not self.hero1.dead:
			if kp_events[pygame.K_LEFT] or kp_events[pygame.K_a]:
				self.hero1.acceptDecision(DECISIONS.MOVE_LEFT)
			if kp_events[pygame.K_RIGHT] or kp_events[pygame.K_d]:
				self.hero1.acceptDecision(DECISIONS.MOVE_RIGHT)
			if kp_events[pygame.K_DOWN] or kp_events[pygame.K_s]:
				self.hero1.acceptDecision(DECISIONS.MOVE_DOWN)
			if kp_events[pygame.K_UP] or kp_events[pygame.K_w]:
				self.hero1.acceptDecision(DECISIONS.MOVE_UP)
			if kp_events[pygame.K_LSHIFT]:
				self.hero1.acceptDecision(DECISIONS.RUN)
				print 'run!!!'
		for event in events:
			if event.type == pygame.KEYDOWN:
				if  not self.hero1.dead and event.key == pygame.K_SPACE:
					self.hero1.acceptDecision(DECISIONS.JUMP)
				if  not self.hero1.dead and event.key==pygame.K_RCTRL:
					print "drag"
					self.hero1.acceptDecision(DECISIONS.DRAG_START)
				if not self.hero1.dead and (event.key==pygame.K_DOWN or event.key==pygame.K_s):
					self.hero1.acceptDecision(DECISIONS.MOVE_DOWN_START)
				if event.key==pygame.K_ESCAPE or event.key==pygame.K_LMETA:
					#PM_request=True
					self.switch_mode(self.pause_submenu)
				else:
					print "ek=",event.key
			elif event.type == pygame.MOUSEBUTTONDOWN:
				#print "event.key",event.button
				if  not self.hero1.dead and event.button == 1:
					self.hero1.acceptDecision(DECISIONS.STANDART_ATTACK)
				if  not self.hero1.dead and event.button == 3:
					self.hero1.acceptDecision(DECISIONS.MAGICK_ATTACK_START)
			elif event.type == pygame.MOUSEBUTTONUP:
				if  not self.hero1.dead and event.button == 3:
					self.hero1.acceptDecision(DECISIONS.MAGICK_ATTACK_STOP)
			'''elif event.type == pygame.KEYUP:
				if  not self.hero1.dead and event.key==pygame.K_RCTRL:
					self.hero1.acceptDecision(DECISIONS.DRAG_STOP)'''
		#if PM_request:
		#	self.switch_mode(self.pause_submenu)
		self.testRoom.process()
		# here is a graffical part
		#self.camera1.recenterCamera([self.hero1])
		self.testRoom.draw(self.gears.screen,self.camera1)
		self.gears.screen.blit(aim,(mouse_pos[0]-0.5*aim.get_width(),mouse_pos[1]-0.5*aim.get_height()))
		char=self.hero1.getActingSelf()
		if char.cur_health>0:
			hb=healthbar_image.copy()
			hb.set_colorkey((255,0,255))
			if char.cur_health!=char.max_health:
				l=hb.get_width()*char.cur_health/char.max_health
				pygame.draw.rect(hb,(255,0,255),pygame.Rect(l,0,hb.get_width()-l,hb.get_height()))
			self.gears.screen.blit(hb,[1,1])
		pygame.display.flip()
		if self.hero1.dead:
			self.activate()
		pygame.time.wait(1000/24)
		#pygame.time.wait(3000/24)
	def configure_window(self):
		size=self.gears.screen_size
		self.camera1.w=size[0]
		self.camera1.h=size[1]
		self.window_size=size